import requests
import re
from lxml import etree
# book模块主要是获取数据并存储数据   获取:http和私人服务器(包括百度网盘)  存储:本地和私人服务器(包括百度网盘)
# book又分为小说,网文,书籍,论文,题目
class Book():
    def __init__(self):
        print("nothing")
    def novel(self):
        print("nothing")

    def onlineBook_Search(self,bookname):
        # 用户需要输入的书籍信息：书名  获取最新章节   获取最近几章  获取全部
        # 请求头和url
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/91.0.4472.77 Safari/537.36 '
        }
        params = {
            'searchkey': bookname
        }
        url = "https://www.biquwx.la/modules/article/search.php"
        # 根据请求头和url，发起get请求。获得主页面的网页结构
        page_text = requests.get(url=url, headers=headers, params=params).content.decode('utf-8')
        fileName = bookname + '.html'
        # 使用xpath进行数据解析
        tree = etree.HTML(page_text)
        chapter_name = tree.xpath('//*[@id="wrapper"]/table//tr[2]/td[2]/a/text()')[0] + '.txt'
        chapter_url = 'https://www.biquwx.la' + tree.xpath('//*[@id="wrapper"]/table//tr[2]/td[2]/a/@href')[0]
        chapter_text = requests.get(url=chapter_url, headers=headers).content.decode('utf-8')
        secondTree = etree.HTML(chapter_text)
        textlist = secondTree.xpath('//*[@id="content"]/text()')
        with open(chapter_name, 'w', encoding='utf-8') as fp:
            for i, text in enumerate(textlist):
                if (i == 0):
                    fp.write('  ' + text[6:] + '\n')
                else:
                    fp.write('  ' + text[4:] + '\n')
    def onlineBook_Recommend(self,hobby):
        # 网文推荐来源两个部分:起点   纵横
        # 推荐根据两个指标: 用户爱好+网上推荐      可以进行深度学习的推荐
        # 推荐的来源网站是起点,纵横,晋江   今日推荐和最新书籍都是根据算法推荐的   热门书籍根据畅销版推荐  推荐不重复和刷新功能
        # 先获取起点的相关信息   hobby是用户爱好,通过用户行为得到。hobby是一个对象,存储了用户的爱好
        # 返回值就是数据接口,是一个对象
        if not hobby:
            # 返回值  每个元素表示一本推荐的书籍信息
            onlineBook=[]

            # 起点(阅文旗下)
            # 根据headers和url，发起get请求。获得主页面的网页结构
            headers_qidian = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            }
            url_qidian = "https://www.qidian.com/rank/signnewbook/"
            page_text_qidian = requests.get(url=url_qidian, headers=headers_qidian).content.decode('utf-8')
            tree_qidian = etree.HTML(page_text_qidian)
            # 根据Html树，获取书籍名称和封面
            # 书名称和书图片地址是一一对应的  统一封装到嵌套列表中
            onlineBook_name_qidian=tree_qidian.xpath('//*[@id="book-img-text"]/ul/li/div[2]/h4/a/text()')
            onlineBook_imageurl_qidian=tree_qidian.xpath('//*[@id="book-img-text"]/ul/li/div[1]/a/img/@src')
            for i in range(len(onlineBook_imageurl_qidian)):
                onlineBook_imageurl_qidian[i]='https:'+onlineBook_imageurl_qidian[i]
                onlineBook.append([onlineBook_name_qidian[i],onlineBook_imageurl_qidian[i],'qidian'])


            # 纵横
            # 根据headers和url，发起get请求。获得主页面的网页结构
            headers_zongheng = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            }
            url_zongheng = "http://www.zongheng.com/rank/details.html?rt=9&d=1"
            page_text_zongheng = requests.get(url=url_zongheng, headers=headers_zongheng).content.decode('utf-8')
            tree_zongheng = etree.HTML(page_text_zongheng)
            # 根据Html树，获取书籍名称和封面
            # 书名称和书图片地址是一一对应的
            onlineBook_imageurl_zongheng=tree_zongheng.xpath('/html/body/div[2]/div[4]/div[2]/div[2]/div/div[1]/a/img/@src')
            onlineBook_name_zongheng=tree_zongheng.xpath('/html/body/div[2]/div[4]/div[2]/div[2]/div/div[2]/div[1]/a/text()')
            for i in range(len(onlineBook_imageurl_zongheng)):
                onlineBook.append([onlineBook_name_zongheng[i],onlineBook_imageurl_zongheng[i],'zongheng'])


            # 晋江
            # 根据headers和url，发起get请求。获得主页面的网页结构
            headers_jjwxc = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            }
            url_jjwxc = "https://www.jjwxc.net/fenzhan/yc/"
            page_text_jjwxc = requests.get(url=url_jjwxc, headers=headers_jjwxc).content.decode('GB18030')
            tree_jjwxc = etree.HTML(page_text_jjwxc)
            # 根据Html树，获取书籍名称和封面
            # 书名称和书图片地址是一一对应的
            # onlineBook_imageurl_jjwxc通过onlineBook_bookurl_jjwxc得到
            onlineBook_imageurl_jjwxc=[]
            onlineBook_bookurl_jjwxc = tree_jjwxc.xpath('//*[@id="mmain2"]/li[1]/dl/dt/a/@href')
            onlineBook_name_jjwxc = tree_jjwxc.xpath('//*[@id="mmain2"]/li[1]/dl/dt/a/text()')
            for i in range(len(onlineBook_name_jjwxc)):
                onlineBook_bookurl_jjwxc[i] = 'https:' + onlineBook_bookurl_jjwxc[i]
                page_text_detail_jjwxc=requests.get(url=onlineBook_bookurl_jjwxc[i], headers=headers_jjwxc).content.decode('GB18030')
                tree_detail_jjwxc = etree.HTML(page_text_detail_jjwxc)
                onlineBook_imageurl_jjwxc.append(tree_detail_jjwxc.xpath('/html/body/table[1]//tr/td[1]/div[2]/img/@src'))
                onlineBook.append([onlineBook_name_jjwxc[i],onlineBook_imageurl_jjwxc[i],'jjwxc'])
            return onlineBook

        else:
            print("暂时还没写")

    def onlineBook_New(self):
        print("nothing")
    def book(self):
        print("nothing")
    def paper(self):
        print("nothing")
    def topic(self):
        print("nothing")
space=None
Book().onlineBook_Recommend(space)



